﻿var links=top.location.href.substring(7).split("/");
var basePath="http://"+links[0]+"/"+links[1]+"/";
$(document).ready(function () {
    initNavClass();
    initDiggs();
    initSearchForm();
    initChannels();
    initExperts();
});


function initNavClass() {
    var path = location.pathname.toLowerCase();
    if (path == "/" || path.indexOf("index.html") > -1) {
        return;
    } else if (path.indexOf("/column") > -1) {
        $(".nav_list a:eq(1)").addClass("select");
    } else if (path.indexOf("experts.html") > -1) {
        $(".nav_list a:eq(2)").addClass("select");
    } else if (path.indexOf("hot.html") > -1) {
        $(".nav_list a:eq(3)").addClass("select");
    } else if (path.indexOf("ranking.html") > -1) {
        $(".nav_list a:eq(4)").addClass("select");
    } else if (path.indexOf("help.html") > -1) {
        $(".nav_list a:eq(5)").addClass("select");
    }
}

function initSearchForm() {
    $("#searchForm").submit(function () {
        var key = $.trim($("#searchKey").val());
        if (!key) {
            alert("请输入要搜索的关键字后再按回车。");
            $("#searchKey").focus();
            return false;
        }
        $("#searchQ").val(key);
    });
}

function initChannels() {
    $(".side_nav a").each(function () {
        if ($(this).attr("name") == "honour") {
            $(this).parent().remove();
        }
        $(this).addClass($(this).attr("name"));
    });
    $(".side_nav li:last").addClass("last");

    var currentChannel = $(".side_nav .select a").attr("name");
    $(".side_nav a").mouseover(function () {
        if ($(this).attr("name") == currentChannel) return;
        $(".side_nav li").each(function () {
            if ($("a", this).attr("name") != currentChannel) {
                $(this).removeClass("select")
            }
        });
        $(this).parent().addClass("select");
    }).mouseout(function () {
        if ($(this).attr("name") != currentChannel)
            $(this).parent().removeClass("select");
    });
}

function initDiggs() {

    var username = $.cookie("userId");

    var digged = [];
    var cookieval = $.cookie("digged");
    if (cookieval) {
        digged = cookieval.split(",");
    }
    $(".digg").each(function () {
        var $this = $(this);
        var id = $this.attr("id");
        var articleId = id.replace("digg_", "");
        var diggNum = $this.attr("digg");
        var buryNum = $this.attr("bury");
        if (digged.contain(articleId)) {
            //顶过了
            $this.html('<span class="up">顶</span><span>(' + diggNum + ')</span><span class="trample">踩</span><span>(' + buryNum + ')</span>')
        } else {
            $this.html('<a href="#up" class="up">顶<span>(' + diggNum + ')</span></a><a href="#trample" class="trample">踩<span>(' + buryNum + ')</span></a>')
        }
    });
    $(".digg a").click(function () {

        if (!username) {
            if (confirm("您的操作必须登录，是否登录？")) {
                top.location.href = basePath+"login.jsp?link="+encodeURIComponent("learning/learning.jsp");
                return false;
            }
            return false;
        }
        var parent = $(this).parent();
        var articleId = parent.attr("id").replace("digg_", "");
        var action = $(this).attr("class");
        $.get(basePath+"DiggedServlet?blogId=" + articleId + "&userId=" + username + "&action=" + action, function (txt) {
//            var data = eval("(" + txt + ")");
            if (txt) {
                var diggNum = parseInt(parent.attr("digg"));
                var buryNum = parseInt(parent.attr("bury"));
                if (action == "up") {
                    diggNum++;
                } else {
                    buryNum++;
                }
                parent.html('<span class="up">顶</span><span>(' + diggNum + ')</span><span class="trample">踩</span><span>(' + buryNum + ')</span>');
//                if (digged.length > 50) { digged.shift(); }
                digged.push(parseInt(articleId));
                $.cookie("digged", digged.join(),{path:"/"});
            } else {
//                if (data.content != "") {
//                    alert("");
//                }
            }
        });
        return false;
    });
}

function initExperts() {
    if ($("#more_experts").length == 0) return;
    $("#btnShowMoreExperts").toggle(function () {
        $("#more_experts").slideDown();
        $(this).removeClass("show").addClass("hidden");
        $(this).text("收起");
    }, function () {
        $("#more_experts").slideUp();
        $(this).removeClass("hidden").addClass("show");
        $(this).text("显示全部专家")
    });
}

function openWindow(url) {
    var f = function () {
        var left = (screen.width - 600) / 2;
        var top = (screen.height - 450) / 2;
        if (!window.open(url, '', 'toolbar=0,resizable=1,scrollbars=yes,status=1,width=600,height=400'))
            location.href = url;
    }
    if (/Firefox/.test(navigator.userAgent)) setTimeout(f, 0); else f();
}